package dao

import (
	"context"
	"device-admin/internal/dal"
	"device-admin/internal/models"
	"strings"

	"192.168.1.75/go-pkg/helper"
	"gorm.io/gorm"
)

type Material struct{}

func (Material) Create(ctx context.Context, db *gorm.DB, merchantID string, materialID int64, name, content, filePath, userID, userName, folderID string, Type, status, auditStatus int) (err error) {
	m := dal.Use(db).Material
	var d models.Material
	d.ID, _ = helper.UUID{}.String()
	d.MerchantID = merchantID
	d.MaterialID = materialID
	d.Name = name
	d.Content = content
	d.FilePath = filePath
	d.CreateUser = userID
	d.CreateUsername = userName
	d.FolderID = folderID
	d.Type = Type
	d.Status = status
	d.AuditStatus = auditStatus
	m.WithContext(ctx).Create(&d)
	return
}

// 获取详情
func (Material) Detail(ctx context.Context, db *gorm.DB, merchantID, ID string, folderID string, Type, status, auditStatus int) (data *models.Material, count int64) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if ID != "" {
		tx = tx.Where(m.ID.Eq(ID))
	}
	if folderID != "" {
		if strings.Contains(folderID, ",") {
			tx = tx.Where(m.FolderID.In(strings.Split(folderID, ",")...))
		} else {
			tx = tx.Where(m.FolderID.Eq(folderID))
		}
	}
	if Type != 0 {
		tx = tx.Where(m.Type.Eq(Type))
	}
	if status != 0 {
		tx = tx.Where(m.Status.Eq(status))
	}
	if auditStatus != -1 {
		tx = tx.Where(m.AuditStatus.Eq(auditStatus))
	}
	count, _ = tx.Count()
	info, _ := tx.Take()
	return info, count
}

// 获取下一个素材ID
func (Material) GetNextMaterialID(ctx context.Context, db *gorm.DB, merchantID string, Type int64) int64 {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	var LatestMaterial, NextMaterialID int64
	if Type == 1 || Type == 2 { // 文本、语音
		tx = tx.Where(m.Type.In([]int{1, 2}...))
		NextMaterialID = 10001
	} else if Type == 3 { // 表情
		tx = tx.Where(m.Type.Eq(3))
		NextMaterialID = 40001
	}
	tx.Order(m.MaterialID.Desc()).
		Select(m.MaterialID).
		Limit(1).
		Scan(&LatestMaterial)
	if LatestMaterial != 0 {
		NextMaterialID = LatestMaterial + 1
	}
	return NextMaterialID
}

// 启用禁用素材
func (Material) ChangeStatus(ctx context.Context, db *gorm.DB, merchantID, ID string, status int) (err error) {
	m := dal.Use(db).Material
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Where(m.MerchantID.Eq(merchantID)).
		Update(m.Status, status)
	return
}

// 素材移动分组
func (Material) ChangeFolder(ctx context.Context, db *gorm.DB, merchantID, ID string, folderID string) (err error) {
	m := dal.Use(db).Material
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Where(m.MerchantID.Eq(merchantID)).
		Update(m.FolderID, folderID)
	return
}

// 素材审核
func (Material) Audit(ctx context.Context, db *gorm.DB, ID string, auditStatus int) (err error) {
	m := dal.Use(db).Material
	m.WithContext(ctx).
		Where(m.ID.Eq(ID)).
		Update(m.AuditStatus, auditStatus)
	return
}

// 素材列表
func (Material) List(ctx context.Context, db *gorm.DB, merchantID, keywords string, Type interface{}, status int, auditStatus int, folderID string, page, limit int) (data []*models.Material, total int64, err error) {
	m := dal.Use(db).Material
	tx := m.WithContext(ctx)
	if merchantID != "" {
		tx = tx.Where(m.MerchantID.Eq(merchantID))
	}
	if keywords != "" {
		tx = tx.Where(
			tx.Where(m.Name.Like("%" + keywords + "%")).
				Or(m.Content.Like("%" + keywords + "%")),
		)
	}
	if Type != nil {
		switch typeV := Type.(type) {
		case int:
			if typeV != 0 {
				tx = tx.Where(m.Type.Eq(typeV))
			}
		case []int:
			if len(typeV) > 0 {
				tx = tx.Where(m.Type.In(typeV...))
			}
		}
	}
	if status != 0 {
		tx = tx.Where(m.Status.Eq(status))
	}
	if auditStatus != -1 {
		tx = tx.Where(m.AuditStatus.Eq(auditStatus))
	}
	if folderID != "" {
		tx = tx.Where(m.FolderID.Eq(folderID))
	}
	data, total, err = tx.Order(m.MaterialID).
		FindByPage(GetOffsetLimit(page, limit))
	return
}
